from timeit import default_timer

import pymysql

host = "localhost"
port = 3306
db = "business"
user = "root"
password = "123456"


def get_connection():
    conn = pymysql.connect(host=host, port=port, db=db, user=user, password=password)
    return conn


class UsingMysql():
    def __init__(self, commit=True, log_time=True, log_label="总用时"):
        self._log_time = log_time
        self._commit = commit
        self._log_label = log_label

    def __enter__(self):
        if self._log_time is True:
            self._start = default_timer()
        conn = get_connection()
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        conn.autocommit = False
        self._conn = conn
        self._cursor = cursor
        return self

    def __exit__(self, *exc_info):
        if self._commit:
            self._conn.commit()
        self._cursor.close()
        self._conn.close()

        if self._log_time is True:
            diff = default_timer()-self._start
            print("——%s:%.6f秒" % (self._log_label, diff))


def check_it():
    with UsingMysql() as um:
        um.cursor.execute("select count(id) as total from business")
        data = um.cursor.fetchone()
        print("--当前记录的个数：%d" % data["total"])


if __name__ == "__main__":
    check_it()
